package cn.wolfcode.controller;



import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Method;
import java.util.*;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private ApplicationContext ctx;

    //给这个方法补充一些信息
    //同时标识这个方法需要权限控制
    //到时候判断的时候也要用到这个注解
    @RequestMapping("/list")
    @RequiresPermissions("permission:list")
    public String list(Model model, QueryObject qo) {
        PageInfo<Permission> pageInfo = permissionService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        return "permission/list";
    }
    @RequiresPermissions("permission:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")

    public String saveOrUpdate(Permission permission) {
        if (permission.getId() == null) {
            permissionService.insert(permission);
        } else {
            permissionService.update(permission);
        }

        return "redirect:/permission/list";
    }

//    @RequestMapping("/load")
//    @ResponseBody
//    public JsonResult load() {
//       try {
//       List<Permission> permissions = permissionService.selectList();
//            LinkedHashSet<Permission> permissionSet = new LinkedHashSet<>();
//        Collection<Object> controllerObjects = ctx.getBeansWithAnnotation(Controller.class).values();
//       for (Object object : controllerObjects) {
//            Class<?> clz = object.getClass();
//           Method[] methods = clz.getDeclaredMethods();
//            for (Method method : methods) {
//               RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
//               if (annotation != null) {
//                    String name = annotation.name();
//                    String expression = annotation.expression();
//                    Permission permission = new Permission();
//                  permission.setName(name);
//                    permission.setExpression(expression);
//                    if (!permissions.contains(permission)) {
//                        permissionSet.add(permission);
//                    }
//               }
//            }
//        }
//           if (permissionSet.size()>0) {
//                permissionService.batchSave(permissionSet);
//            }
//        }catch(Exception e){
//            return  new JsonResult(false,"权限加载失败");
//        }
//        return new JsonResult(true,"权限加载成功");
//              }
//        try {
//            Set<Permission> permissionSet = ctx.getBeansWithAnnotation(Controller.class).values().stream()
//                    .flatMap(controllerObject -> Arrays.stream(controllerObject.getClass().getDeclaredMethods()))
//                    .map(method -> new Permission(method.getAnnotation(RequiredPermission.class).name(),
//                            method.getAnnotation(RequiredPermission.class).expression()))
//                    .filter(permission -> !permissions.contains(permission))
//                    .collect(Collectors.toSet());
//            if (permissionSet.size() > 0) {
//                permissionService.batchSave(permissionSet);
//            }
//        } catch (Exception e) {
//            return new JsonResult(false, "权限加载失败");
//        }
//        return new JsonResult(true, "权限加载成功");
//        //      }
//    }
    @RequestMapping("/noPermission")
    public String noPermission(){
        return "common/nopermission";
    }
}